package com.hanbang.chu_xing_gao_zhao.view_find_esoterica;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanbang.chu_xing_gao_zhao.application.HttpInterfaces;
import com.hanbang.chu_xing_gao_zhao.base.BaseActivity;
import com.hanbang.chu_xing_gao_zhao.entity.UserInforData;
import com.hanbang.chu_xing_gao_zhao.utils.MyRequestCallBack;
import com.hanbang.chu_xing_gao_zhao.utils.MySharedPreferences;
import com.hanbang.chu_xing_gao_zhao.utils.MyToast;
import com.hanbang.chu_xing_gao_zhao.view_city_select.ProvinceSelectActivity;
import com.hanbang.cxgz_2.R;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONObject;

/**
 * 报名页面
 */
public class FindEsotericaSignUpActivity extends BaseActivity {
    private LinearLayout city;
    private EditText name;
    private EditText contact;
    private EditText company;
    private String provinceId = "";
    private TextView uploading;
    private String provincename = "";
    private int esotericaId;
    private UserInforData user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_sign_up_activity);
        initView();
        setOnClickListener();
    }

    /**
     * 初始化view
     */
    private void initView() {
        //标题设置
        setTitleMore(View.VISIBLE, View.INVISIBLE, "众筹买断", null, null, null);
        setBackArrows(findViewById(R.id.bar_top_conver_classify_back));

        city = (LinearLayout) findViewById(R.id.ll_find_sign_up_city);
        name = (EditText) findViewById(R.id.et_find_sign_up_name);
        contact = (EditText) findViewById(R.id.et_find_sign_up_contact);
        company = (EditText) findViewById(R.id.et_find_sign_up_company);
        name = (EditText) findViewById(R.id.et_find_sign_up_name);
        uploading = (TextView) findViewById(R.id.tv_find_sign_up_uploading);

        Intent intent = getIntent();
        esotericaId = intent.getIntExtra("esotericaId", -1);

//        MyLog.i("text","esotericaId****"+esotericaId);


        //查询数据库，把用记基本信息填上。
        try {
            user = dbUtils.findById(UserInforData.class, userData.getId());
            name.setText(user.getUser_name());
            contact.setText(user.getTelphone());

        } catch (DbException e) {
            e.printStackTrace();
        }


    }

    /**
     * 设置点击事件
     */
    private void setOnClickListener() {
        city.setOnClickListener(onClickListener);
        uploading.setOnClickListener(onClickListener);
    }

    /**
     * 处理监听
     * company：工作单位
     * contact：联系方式
     * id：秘籍的id
     * name：姓名
     * province_id：省份id
     * province_name：省份名称
     */
    @SuppressWarnings("unused")
    private OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            switch (v.getId()) {
                case R.id.ll_find_sign_up_city:
                    intent.setClass(getApplicationContext(), ProvinceSelectActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivityForResult(intent, 99);

                    break;
                case R.id.tv_find_sign_up_uploading:
                    if (name.getText().toString().equals("")) {
                        MyToast.show(FindEsotericaSignUpActivity.this, "姓名不能为空", 1);
                        return;
                    }
                    if (contact.getText().toString().equals("")) {
                        MyToast.show(FindEsotericaSignUpActivity.this, "联系方式不能为空", 1);
                        return;
                    }
                    if (provincename.equals("")) {
                        MyToast.show(FindEsotericaSignUpActivity.this, "城市不能为空", 1);
                        return;
                    }


                    RequestParams params = new RequestParams();
                    params.addBodyParameter("telphone", MySharedPreferences.getSharedPreferencesKeyAndValue(
                            FindEsotericaSignUpActivity.this, MySharedPreferences.USER_DATA, "tlephone"));
                    params.addBodyParameter("password", MySharedPreferences.getSharedPreferencesKeyAndValue(
                            FindEsotericaSignUpActivity.this, MySharedPreferences.USER_DATA, "password"));

                    params.addBodyParameter("company", company.getText().toString());
                    params.addBodyParameter("contact", contact.getText().toString());
                    params.addBodyParameter("id", esotericaId + "");
                    params.addBodyParameter("name", name.getText().toString());
                    params.addBodyParameter("province_id", provinceId);
                    params.addBodyParameter("province_name", provincename);

                    uploadingHttpSignUp(HttpInterfaces.miji_baoming, params);

                default:
                    break;
            }
        }
    };


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        /**
         得到返回过来的城市
         */
        if (requestCode == 99 && resultCode == RESULT_OK) {

            provincename = data.getStringExtra("provincename");
            provinceId = data.getStringExtra("provinceId");

            ((TextView) this.city.getChildAt(0)).setText(provincename);

        }

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }


    private void uploadingHttpSignUp(String url, RequestParams params) {
        httpUtils.send(HttpRequest.HttpMethod.POST, url, params, new MyRequestCallBack<String>(this, "加载中...") {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                super.onSuccess(responseInfo);
                try {
                    JSONObject json = new JSONObject(responseInfo.result);
                    int request = json.getInt("Result");
                    String msg=json.getString("Msg");

                    if (request==0) {
                        MyToast.show(FindEsotericaSignUpActivity.this, "申请成功", 1);
                        finish();
                    } else {
                        MyToast.show(FindEsotericaSignUpActivity.this, msg, 1);
                        finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    MyToast.show(FindEsotericaSignUpActivity.this, "服务忙请稍后重试", 1);

                }


            }
        });


    }
}
